#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int maxRotateFunction(vector<int>& nums) {
        int n = nums.size(), ret = 0;
        vector<int> sum(n), dp(n);
        sum[0] = nums[0];
        for (int i = 1; i < n; i++)
        {
            sum[i] = nums[i] + sum[i - 1];
            dp[0] += nums[i] * i;
        }
        ret = dp[0];
        for (int i = 1; i < n; i++)
        {
            dp[i] = dp[i - 1] - (sum[n - 1] - nums[i - 1]) + (n - 1) * nums[i - 1];
            ret = max(dp[i], ret);
        }
        return ret;
    }
};